package com.utbm.lo54.controllers;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.utbm.lo54.dao.mysql.MysqlClientDAO;
import com.utbm.lo54.dao.mysql.MysqlCourseDAO;
import com.utbm.lo54.dao.mysql.MysqlLocationDAO;
import com.utbm.lo54.dao.mysql.MysqlSessionDAO;
import com.utbm.lo54.dao.neo4j.Neo4jClientDAO;
import com.utbm.lo54.entity.Client;
import com.utbm.lo54.entity.Course;
import com.utbm.lo54.entity.Location;
import com.utbm.lo54.entity.Session;

/**
 * Servlet implementation class InscriptionServlet
 */
@WebServlet("/InscriptionServlet")
public class InscriptionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InscriptionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Long sessionid = Long.parseLong(request.getParameter("sessionid"));
		
		MysqlSessionDAO sessionDAO = new MysqlSessionDAO();
		MysqlLocationDAO locationDAO = new MysqlLocationDAO();
		MysqlCourseDAO courseDAO = new MysqlCourseDAO();
		
		Session session = sessionDAO.findById(sessionid);
		Course course = courseDAO.findByCode(session.getCourseCode());
		Location location = locationDAO.findById(session.getLocationId());
		
		
		request.setAttribute("session", session);
		request.setAttribute("course", course);
		request.setAttribute("location", location);
		RequestDispatcher dis=request.getRequestDispatcher("Inscription.jsp");
		dis.forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String firstname = request.getParameter("firstname");
		String lastname =request.getParameter("lastname");
		String address =request.getParameter("address");
		String phone =request.getParameter("phone");
		String email =request.getParameter("email");
		Long sessionId =Long.parseLong(request.getParameter("sessionid"));
		
		System.out.println(firstname);
		
		if(firstname == "" || lastname == "" || address == "" || phone == "") {
			
			RequestDispatcher dis=request.getRequestDispatcher("Failed.jsp");
			dis.forward(request,response);
			
		}
		
		else {
			
			try{
				Client client =new Client(firstname,lastname,email,address,phone);
				client.setSessionId(sessionId);
				MysqlClientDAO clientDAO = new MysqlClientDAO();
				Neo4jClientDAO clientNeo4jDAO = new Neo4jClientDAO();
				clientDAO.create(client);
				clientNeo4jDAO.create(client);
				
				RequestDispatcher dis=request.getRequestDispatcher("Success.jsp");
				dis.forward(request,response);
				}
				catch(Exception e){
					RequestDispatcher dis=request.getRequestDispatcher("Failed.jsp");
					dis.forward(request,response);
				}
		}
		

	}
}
